﻿Imports CrystalDecisions.Shared

Public Class MainFinanceReport
    Dim DAReservation As New DSReservationTableAdapters.V_RESERVATIONTableAdapter
    Dim DAReservationDetail As New DSReservationTableAdapters.RESERVATION_DETAILTableAdapter
    Dim DARoom As New DSFloorManagemantTableAdapters.ROOMSTableAdapter
    Dim DARe As New DSReservationTableAdapters.RESERVATIONTableAdapter
    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        With cboRoom
            .DataSource = DARoom.GetData
            .ValueMember = "ROOM_ID"
            .DisplayMember = "ROOM_NAME"
            .SelectedIndex = -1
        End With
        ' Add any initialization after the InitializeComponent() call.

    End Sub
    Private Sub BtnFrom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnFrom.Click
   
        SplitContainer2.Visible = True
        SplitContainer2.Dock = DockStyle.Fill
        PDFFinanceReport.Visible = False

        If ChShowRoom.Checked = True Then
            ViewFinanceReportByRoom(2, cboRoom.SelectedValue)
        Else
            If ChIsDelete.Checked = True Then
                ViewFinaceReportIsDelete()
            Else
                ViewFinaceReport(2, 1)
            End If

        End If

    End Sub
    Private Sub ViewFinaceReport(ByVal CheckInCheckOut As Integer, ByVal IsDelete As Integer)

        GridReservation.DataSource = DAReservation.SelectFinanceReport(DateFrom.Value.Date, DateTo.Value.Date, CheckInCheckOut, IsDelete)


    End Sub
    Private Sub ViewFinaceReportIsDelete()
        GridReservation.DataSource = DAReservation.SelectIsDelete(DateFrom.Value.Date, DateTo.Value.Date, 2)
    End Sub
    Private Sub ViewFinanceReportByRoom(ByVal statusID As Integer, ByVal roomID As Integer)
        GridReservation.DataSource = DAReservation.SelectByroom(DateFrom.Value.Date, DateTo.Value.Date, statusID, roomID)
    End Sub
    Public Sub RefreshReservationDetail(ByVal ReservationID As Integer)
        GridReservationDedail.DataSource = DAReservationDetail.SelectReservationByID(ReservationID)
    End Sub

    Private Sub GridReservation_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridReservation.SelectionChanged
        Try
            RefreshReservationDetail(GridReservation.GetRow.Cells("RESERVATION_ID").Value)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub BtnShowReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnShowReport.Click
        SplitContainer2.Visible = False
        PDFFinanceReport.Visible = True
        PDFFinanceReport.Dock = DockStyle.Fill
        Dim RptFinanceReport As New FinanceReporting
        If ChShowRoom.Checked = True Then
            Dim tblFinanceReport As DataTable = DAReservation.SelectByroom(DateFrom.Value.Date, DateTo.Value.Date, 2, cboRoom.SelectedValue)
            RptFinanceReport.SetDataSource(tblFinanceReport)
            RptFinanceReport.SetParameterValue("DateFrom", Format(DateFrom.Value.Date, "dd/MM/yyyy"))
            RptFinanceReport.SetParameterValue("DateTo", Format(DateTo.Value.Date, "dd/MM/yyyy") & " លើបន្ទប់" & " " & cboRoom.Text)
        Else
            Dim tblFinanceReport As DataTable
            If ChIsDelete.Checked = True Then
                tblFinanceReport = DAReservation.SelectIsDelete(DateFrom.Value.Date, DateTo.Value.Date, 2)
                RptFinanceReport.SetDataSource(tblFinanceReport)
                RptFinanceReport.SetParameterValue("DateFrom", Format(DateFrom.Value.Date, "dd/MM/yyyy"))
                RptFinanceReport.SetParameterValue("DateTo", Format(DateTo.Value.Date, "dd/MM/yyyy") & " ដែលបានលប់ ")
            Else
                tblFinanceReport = DAReservation.SelectFinanceReport(DateFrom.Value.Date, DateTo.Value.Date, 2, 1)
                RptFinanceReport.SetDataSource(tblFinanceReport)
                RptFinanceReport.SetParameterValue("DateFrom", Format(DateFrom.Value.Date, "dd/MM/yyyy"))
                RptFinanceReport.SetParameterValue("DateTo", Format(DateTo.Value.Date, "dd/MM/yyyy"))
            End If

           
        End If
       
        Dim CrExportOptions As ExportOptions
        Dim CrDiskFileDestinationOptions As New DiskFileDestinationOptions()
        Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions()
        CrDiskFileDestinationOptions.DiskFileName = My.Application.Info.DirectoryPath & "\crystalExport.pdf"
        CrExportOptions = RptFinanceReport.ExportOptions
        With CrExportOptions
            .ExportDestinationType = ExportDestinationType.DiskFile
            .ExportFormatType = ExportFormatType.PortableDocFormat
            .DestinationOptions = CrDiskFileDestinationOptions
            .FormatOptions = CrFormatTypeOptions
        End With
        RptFinanceReport.Export()
        Application.DoEvents()
        Application.DoEvents()
        PDFFinanceReport.src = My.Application.Info.DirectoryPath & "\crystalExport.pdf"
        PDFFinanceReport.setZoom(100)
    End Sub

    Private Sub MainFinanceReport_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SplitContainer2.Visible = True
        SplitContainer2.Dock = DockStyle.Fill
        PDFFinanceReport.Visible = False

        If ChShowRoom.Checked = True Then
            ViewFinanceReportByRoom(2, cboRoom.SelectedValue)
        Else
            If ChIsDelete.Checked = True Then
                ViewFinaceReport(2, 2)
            Else
                ViewFinaceReport(2, 1)
            End If

        End If

    End Sub

    Private Sub ChShowRoom_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChShowRoom.CheckedChanged
        cboRoom.Enabled = ChShowRoom.Checked
        cboRoom.SelectedIndex = -1
    End Sub

    Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        If GridReservation.SelectedItems.Count = 0 Then Exit Sub
        If MessageBox.Show("តើអ្នកចង់លប់ការស្នាក់មួយនេះ?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            DARe.IsDeleteOrUndo(2, GridReservation.GetRow.Cells("RESERVATION_ID").Value)
            SplitContainer2.Visible = True
            SplitContainer2.Dock = DockStyle.Fill
            PDFFinanceReport.Visible = False

            If ChShowRoom.Checked = True Then
                ViewFinanceReportByRoom(2, cboRoom.SelectedValue)
            Else
                If ChIsDelete.Checked = True Then
                    ViewFinaceReport(2, 2)
                Else
                    ViewFinaceReport(2, 1)
                End If

            End If
        End If
    End Sub

    Private Sub BtnUndo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnUndo.Click
        If GridReservation.SelectedItems.Count = 0 Then Exit Sub
        If MessageBox.Show("តើអ្នកចង់លប់ការស្នាក់មួយនេះ?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            DARe.IsDeleteOrUndo(1, GridReservation.GetRow.Cells("RESERVATION_ID").Value)
            SplitContainer2.Visible = True
            SplitContainer2.Dock = DockStyle.Fill
            PDFFinanceReport.Visible = False

            If ChShowRoom.Checked = True Then
                ViewFinanceReportByRoom(2, cboRoom.SelectedValue)
            Else
                If ChIsDelete.Checked = True Then
                    ViewFinaceReport(2, 2)
                Else
                    ViewFinaceReport(2, 1)
                End If

            End If
        End If
    End Sub

    Private Sub btnEdite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdite.Click
        Try
            If GridReservation.SelectedItems.Count = 0 Then Exit Sub
            Dim FEditCheckOut As New FormEditCheckOut
            FEditCheckOut.LblReservation.Text = GridReservation.GetRow.Cells("RESERVATION_ID").Value
            FEditCheckOut.lblRoomID.Text = GridReservation.GetRow.Cells("ROOM_ID").Value
            'FEditCheckOut.Text.Text = GridReservation.GetRow.Cells("CUSTOMER_NAME").Value
            FEditCheckOut.TxtRoomNo.Text = GridReservation.GetRow.Cells("ROOM_NAME").Value
            FEditCheckOut.TxtBed.Text = GridReservation.GetRow.Cells("ROOM_BED").Value
            FEditCheckOut.TxtDateIn.Text = GridReservation.GetRow.Cells("CHECK_IN_DATE").Text
            FEditCheckOut.DateOut1.Text = GridReservation.GetRow.Cells("CHECK_OUT_DATE").Text
            FEditCheckOut.TimeIn.Value = GridReservation.GetRow.Cells("TIME_IN").Value
            FEditCheckOut.TimeOut.Value = GridReservation.GetRow.Cells("TIME_OUT").Value

            If FEditCheckOut.ShowDialog() = DialogResult.OK Then
                SplitContainer2.Visible = True
                SplitContainer2.Dock = DockStyle.Fill
                PDFFinanceReport.Visible = False

                If ChShowRoom.Checked = True Then
                    ViewFinanceReportByRoom(2, cboRoom.SelectedValue)
                Else
                    If ChIsDelete.Checked = True Then
                        ViewFinaceReport(2, 2)
                    Else
                        ViewFinaceReport(2, 1)
                    End If

                End If
            End If
        Catch ex As Exception

        End Try
        
    End Sub
End Class
